package cn.seaboot.admin.logger.web;

import cn.seaboot.admin.logger.service.Log4jService;
import cn.seaboot.admin.mvc.Result;
import cn.seaboot.admin.mvc.argument.Params;
import cn.seaboot.commons.file.FilenameUtils;
import cn.seaboot.commons.file.MediaType;
import cn.seaboot.commons.file.ResponseWriter;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Set;

/**
 * 系统日志 [t_sys_log_access] - Controller
 *
 * @author Mr.css 2018年8月22日 上午11:10:21
 */
@Controller
@Tag(name = "系统日志 [log4j] 管理")
@RequestMapping("sys/log/log4j")
public class Log4jCtrl {

    @Resource
    private Log4jService log4jService;
 
    /**
     * 查列表
     *
     * @return PageInfo
     */
    @ResponseBody
    @Operation(description = "系统日志 [log4j] 列表查询")
    @RequestMapping(value = "list", method = RequestMethod.GET)
    public Set<String> queryPage() {
        return log4jService.listFile();
    }

    /**
     * 下载Excel文件
     *
     * @param filename 文件名
     * @throws IOException -
     */
    @Operation(description = "系统日志 [log4j] 下载源文件")
    @Parameters({@Parameter(name = "filename", description = "文件名")})
    @RequestMapping(value = "download", method = RequestMethod.GET)
    public void exportExcel(
            @Params String filename,
            HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = FilenameUtils.getName(filename);
        try (InputStream is = log4jService.openInputStream(filename)) {
            ResponseWriter.download(response, is, name);
        }
    }

    /**
     * 下载Excel文件
     *
     * @param filename 文件名
     * @throws IOException -
     */
    @ResponseBody
    @Operation(description = "系统日志 [log4j] 删除源文件")
    @Parameters({@Parameter(name = "filename", description = "文件名")})
    @RequestMapping(value = "info", method = RequestMethod.DELETE)
    public Result delete(@Params String filename) throws IOException {
        log4jService.deleteFile(filename);
        return Result.succeed();
    }
}
